﻿<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="../helpproject.xsl" ?>
<topic template="Default" lasteditedby="Geert" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../helpproject.xsd">
  <title translate="true">Finding the view of a view model</title>
  <keywords>
    <keyword translate="true">IViewManager</keyword>
    <keyword translate="true">ViewManager</keyword>
  </keywords>
  <body>
    <header>
      <para styleclass="Heading1"><text styleclass="Heading1" translate="true">Finding the view of a view model</text></para>
    </header>
    <para styleclass="Normal"><text styleclass="Normal" translate="true">Sometimes it is required to find the view of a view model. For example, this comes in handy when implementing drag and drop where you only want to support code via view models.</text></para>
    <para styleclass="Normal"><text styleclass="Normal" translate="true">Internally, Catel uses with the </text><link displaytype="text" defaultstyle="true" type="topiclink" href="T_Catel_MVVM_Views_IViewManager" styleclass="Normal" translate="true">IViewManager</link><text styleclass="Normal" translate="true"> for this. As soon as a view is loaded (via the Loaded event), the view is registered to the view manager. The view manager will keep an eye on the events of the view and notice view model changes.</text></para>
    <para styleclass="Normal"><text styleclass="Normal" translate="true">A view is removed from the manager as soon as it is unloaded (via the Unloaded event). From this moment on, it is no longer possible to retrieve a view via its view model.</text></para>
    <para styleclass="Notes"><text styleclass="Notes" translate="true">Remember that only controls implementing IView are supported by the IViewManager</text></para>
    <para styleclass="Heading1"><text styleclass="Heading1" translate="true">Retrieving the view of a view model</text></para>
    <para styleclass="Normal"><text styleclass="Normal" translate="true">To find the view of a view model, use the steps below:</text></para>
    <para styleclass="Normal"><text styleclass="Normal" translate="true">1) Resolve the view from </text><link displaytype="text" defaultstyle="true" type="topiclink" href="T_Catel_MVVM_Views_IViewManager" styleclass="Normal" translate="true">IViewManager</link><text styleclass="Normal" translate="true"> from the </text><link displaytype="text" defaultstyle="true" type="topiclink" href="GEN_IoC" styleclass="Normal" translate="true">ServiceLocator</link><text styleclass="Normal" translate="true">:</text></para>
    <para styleclass="Code Example"><text styleclass="Code Example" style="font-weight:normal; font-style:normal; text-decoration:none; color:#000000;" translate="true">var</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; text-decoration:none; color:#ffffff;" translate="true"> </text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; text-decoration:none; color:#000000;" translate="true">viewManager</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; text-decoration:none; color:#ffffff;" translate="true"> </text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; text-decoration:none; color:#000000;" translate="true">=</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; text-decoration:none; color:#ffffff;" translate="true"> </text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; text-decoration:none; color:#000000;" translate="true">ServiceLocator.Instance.ResolveType&lt;IViewManager&gt;();</text></para>
    <para styleclass="Normal"><text styleclass="Normal" translate="true">2) Resolve the view:</text></para>
    <para styleclass="Code Example"><text styleclass="Code Example" style="font-weight:normal; font-style:normal; text-decoration:none; color:#000000;" translate="true">var</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; text-decoration:none; color:#ffffff;" translate="true"> </text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; text-decoration:none; color:#000000;" translate="true">views</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; text-decoration:none; color:#ffffff;" translate="true"> </text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; text-decoration:none; color:#000000;" translate="true">=</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; text-decoration:none; color:#ffffff;" translate="true"> </text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; text-decoration:none; color:#000000;" translate="true">viewManager.GetViewsOfViewModel(myViewModel);</text></para>
    <para styleclass="Notes"><text styleclass="Notes" translate="true">Note that it is possible that multiple views are linked to the same view model</text></para>
  </body>
</topic>
